﻿using System;
using System.Windows.Forms;
using System.Xml.Linq;

namespace Microsoft.Office.OneNote.XmlViewer.Controls
{
    public partial class ContentViewer : UserControl
    {
        private Views.ViewBase _currentView;
        private OneNoteHierarchy _currentHierarchy;
        private OneNoteHierarchyObject _currentObject;

        public event EventHandler StatusTextChanged;

        public ContentViewer()
        {
            InitializeComponent();
            _currentView = viewXml;
        }

        public void Navigate(XDocument document)
        {
            _currentObject = null;
            _currentHierarchy = null;
            _currentView.Navigate(document);
        }

        public void Navigate(OneNoteHierarchy hierarchy)
        {
            _currentObject = null;
            _currentHierarchy = hierarchy;
            _currentView.Navigate(hierarchy);
        }


        public void Navigate(OneNoteHierarchyObject obj)
        {
            _currentObject = obj;
            _currentHierarchy = null;
            _currentView.Navigate(obj);
        }

        public void LeaveCurrentObject()
        {
            _currentObject = null;
            _currentHierarchy = null;
            _currentView.LeaveCurrentObject();
        }

        public string StatusText
        {
            get { return _currentView.StatusText; }
        }

        protected override void OnLoad(EventArgs e)
        {
            Navigate((OneNoteHierarchyObject)(null));
            base.OnLoad(e);
        }

        private void OnViewChanged(object sender, EventArgs e)
        {
            viewXml.Visible = (switcher.CurrentView == ViewType.Xml);
            viewHtml.Visible = (switcher.CurrentView == ViewType.Html);
            viewObject.Visible = (switcher.CurrentView == ViewType.Objects);

            switch (switcher.CurrentView)
            {
                case ViewType.Objects:
                    ChangeCurrentView(viewObject);
                    break;

                case ViewType.Xml:
                    ChangeCurrentView(viewXml);
                    break;

                case ViewType.Html:
                    ChangeCurrentView(viewHtml);
                    break;
                default:
                    break;
            }
        }

        private void ChangeCurrentView(Views.ViewBase view)
        {
            if (_currentView != view)
            {
                _currentView.StatusTextChanged -= OnStatusTextChanged;
                
                _currentView = view;
                _currentView.Dock = DockStyle.Fill;

                if (_currentObject != null)
                {
                    _currentView.Navigate(_currentObject);
                }
                else if (_currentHierarchy != null)
                {
                    _currentView.Navigate(_currentHierarchy);
                }

                _currentView.StatusTextChanged += OnStatusTextChanged;
            }
        }

        private void OnStatusTextChanged(object sender, EventArgs e)
        {
            if (StatusTextChanged != null)
            {
                StatusTextChanged(this, e);
            }
        }

        private void switcher_Load(object sender, EventArgs e)
        {

        }
    }
}
